﻿<%@ page language="java" import="com.ruohi.util.common.CnToFullSpell,com.ruohi.util.common.CommonForm"
		 pageEncoding="UTF-8" %>
<%@ page import="com.ruohi.util.common.CommonString" %>
<%@ page import="com.ruohi.util.common.Env" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.TreeMap" %>
<%
	String tablePrefix = Env.getInstance().getProperty("table_prefix");
	String tableName = "gift_product";
	String channelUUID = CommonString.getFormatPara(request.getParameter("channel_uuid"));
	String fileds = "id,uuid,no,name,category,brand,storage,weight,view_count,is_show,order_no,is_rec_index,is_buy,(select count(*) from gift_product_comment where gift_product_comment.product_no=gift_product.no) as comment_count,(select count(*) from gift_product_ask where gift_product_ask.product_no=gift_product.no) as ask_count,(select count(*) from gift_product_keyword where gift_product_keyword.product_no=gift_product.no) as keyword_count";
	String whereStr = "";
	String orderByStr = " id desc ";
	
	List<Map> brandList = (ArrayList)application.getAttribute("giftBrandList");
	//产品类别
	Map categoryMap = new TreeMap();
	List<Map> categoryList = (List)application.getAttribute("giftCategoryList");
	String n = "";
	String name = "";
	if(categoryList != null){
		for(Map m : categoryList){
			n = (String)m.get("no");
			name = (String)m.get("name");
			name = CommonForm.getHtmlSpaceByString((String)m.get("parent_no"))+name;
			categoryMap.put(n,name);
		}
	}
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title><%=Env.getInstance().getProperty("project_name") %></title>
	<link href="/plugin/ui/ligerUI/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
	<link href="/plugin/ui/ligerUI/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
	<link href="/plugin/ui/ligerUI/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
	<link href="/backend/style/style.css" rel="stylesheet" type="text/css" />
	<script src="/plugin/jquery/jquery.min.js" type="text/javascript"></script>
  </head>
  <body>
  	<div class="searchForm">
  		<div class="searchCondition">
        	<div class="searchLeft">编号</div>
	  		<div class="searchRight">
	  			<input type="text" style="width: 120px;" name="no" class="jr_text" id="no" />
	  		</div>
	  		<div class="searchLeft">名称</div>
	  		<div class="searchRight">
	  			<input type="text" style="width: 120px;" name="name" class="jr_text" id="name" />
	  		</div>
            <div class="searchLeft">类别</div>
	  		<div class="searchRight">
	  			<select class="jr_select" id="category" name="category" >
                    <option value="">--请选择类别--</option>
                    <%=CommonForm.createSelect(categoryMap, "") %>
                </select>
	  		</div>
            <div class="searchLeft">品牌</div>
	  		<div class="searchRight">
	  			  <select class="jr_select" id="brand" name="brand">
                    <option value="">-请选择品牌-</option>
                    <%if(brandList != null){for(Map m : brandList){%>
                    <option value="<%=m.get("no")%>" ><%=CnToFullSpell.getFirstSpell(CommonString.getFormatPara(m.get("name"))).length()>=1?CnToFullSpell.getFirstSpell(CommonString.getFormatPara(m.get("name"))).substring(0,1):""%>-<%=CommonString.getFormatPara(m.get("name"))%></option>
                    <%}}%>
                  </select>
	  		</div>
	  		<div class="searchLeft">是否上架</div>
	  		<div class="searchRight">
	  			<select class="jr_select" id="is_show" name="is_show" >
                    <option value="">--请选择--</option>
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
	  		</div>
            <div class="searchLeft">是否推荐</div>
	  		<div class="searchRight">
	  			<select class="jr_select" id="is_rec" name="is_rec" >
                    <option value="">--请选择--</option>
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
	  		</div>
	  		<div class="searchLeft">重量</div>
	  		<div class="searchRight">
	  			<input type="text" style="width: 120px;" name="weight" class="jr_text" id="weight" />
	  		</div>
  		</div>
  		<div class="searchBtn">
  			<input type="button" class="btn" onclick="doSearch();" value="查询" />
  			<input type="button" class="btn" onclick="doClearForm();" value="重置" />
  		</div>
  	</div>
	<div style="clear: left;"></div>
	<div class="l-loading" style="display: block" id="pageloading"></div> 
	<div id="maingrid" style="margin: 0; padding: 0"></div>
	<div style="display: none;"></div>
  </body>
</html>
<script src="/plugin/ui/ligerUI/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="/backend/scripts/common.js" prefix="<%=tablePrefix %>" type="text/javascript"></script>
<script type="text/javascript">
	var grid;
	var tableName = "<%=tableName %>";
	var channelUUID = '<%=channelUUID%>';
	var fields = "<%=fileds %>";
	var whereStr = "<%=whereStr %>";
	var orderBy = "<%=orderByStr %>";
	var columnsArr = [];
	var url = "";
	var conditionStr = "";
	var params = [];
	var myToolbar = {
        items: [{
            text: '增加',
            click: function(){
                addAndEdit(undefined, false);
            },
            icon: 'add'
        },{
        	text: "删除",
        	click: function(){
        		toolbarDel({
					'eventAfter':'deleteProductIndex'
				});
        	},
        	icon: "delete"
        },{
            text: '打开查询',
            click: function(){
                closeAndOpen();
            },
            icon: 'right'
        },{
			text : '新品推荐',
			click : function() {
				setRec("1");
			},
			icon : 'config'
		},{
			text : '取消推荐',
			click : function() {
				setRec("0");
			},
			icon : 'cancelrem'
		}
		,{
			text : '批量显示',
			click : function() {
				setShow("1");
			},
			icon : 'config'
		},{
			text : '批量隐藏',
			click : function() {
				setShow("0");
			},
			icon : 'cancelrem'
		}
		,{
			text : '批量上架',
			click : function() {
				setSale("1");
			},
			icon : 'config'
		},{
			text : '批量下架',
			click : function() {
				setSale("0");
			},
			icon : 'cancelrem'
		}
		/*,{
			text:"生成全部索引",
			click:function(){
				jrConfirm("确定要生成全部索引吗？", function(v){
					$.ajax({
						type:"post",
						url:"/backend/action/ajax_r.jsp",
						data:{flag:"generateAllProductIndex",enumNo:"106013"},
						success:function(result){
							if($.trim(result)=="1"){
								alert("生成成功");
							}
						}
					});
				});
			},
			icon:"config"
		}*/
		]
    };
	jQuery(function() {
		params.push({'name':'enumNo','value':'106013','fileName':'test'});
		grid = new Dev21sunGrid({
			toolbar:myToolbar
			});
		jQuery("#pageloading").hide();
	});
	//列数组
	columnsArr =  [ 
	{ display : '名称', name : 'name'  , id : 'no', align : 'left' , type : 'string' , width : 260 , render : function(row){
        return "<a target='_blank' href='http://www.52sdskj.com/goods/" + row.no + ".htm'><img class='preview' src='/plugin/ui/ligerUI/ligerUI/skins/icons/preview.png' /></a><a class='link' href='javascript:void(0);' onclick='addAndEdit(" + row.__index + ");'>" + row.name + "</a>";
			}
		} ,
	{ display : '编号', name : 'no'  , align : 'left' , type : 'string' , width : 130 , render : function(row){
			return "<a class='link' href='javascript:void(0);' onclick='addAndEdit("+row.__index+");'>"+row.no+"</a>";
		}
	}  ,
	{ display : '类别', name : 'category'  , align : 'left' , type : 'string' , width : 100 , render : function(row){
		return getGiftCategory(row.category,"giftCategoryMap");	
	}} ,
//	{ display : '复制产品', name : 'brand'  , align : 'center' , type : 'string' , width : 80 , render : function(row){
//		return "<a href='javascript:void(0);' onclick='copyProduct("+row.id+");'>复制</a>";
//	}} ,
	{ display : '库存量', name : 'storage'  , align : 'left' , type : 'string' , width : 60 } ,
	{ display:"重量(KG)", name:"weight", align:"center", type:"string", width:80 },
	{ display:"是否上架", align:"center", type:"int", width:60, render:function(row){
		return row.is_buy==1 ? "是" : "<span style='color:red'>否</span>";
	} },
	{ display:"是否显示", align:"center", type:"int", width:60, render:function(row){
		return row.is_show==1 ? "是" : "<span style='color:red'>否</span>";
	} },
	{ display:'评论', align:'center', width:80,render:function(row){
		return "<a href='javascript://void(0);' onclick=\"viewComment(\'" + row.no + "\');\">查看(" + row.comment_count + ")</a>";
	} },
	{ display:'提问', align:'center', width:80,render:function(row){
		return "<a href='javascript://void(0);' onclick=\"viewAsk(\'" + row.no + "\');\">查看(" + row.ask_count + ")</a>";
	} }
//	,
//	{ display:'关键字', align:'center', width:80,render:function(row){
//		return "<a href='javascript://void(0);' onclick=\"viewKeyword(\'" + row.no + "\');\">查看(" + row.keyword_count + ")</a>";
//	} }
	];
	
	function viewComment(product_no){
		jrWindow("/backend/gift.21-sun.com/gift_product_comment/gift_product_comment_list.jsp?product_no=" + product_no, "查看评价", 800, 630);
	}
	
	function viewAsk(product_no){
		jrWindow("/backend/gift.21-sun.com/gift_product_ask/gift_product_ask_list.jsp?product_no=" + product_no, "查看提问", 800, 630);
	}
	
	function viewKeyword(product_no){
		jrWindow("/backend/gift.21-sun.com/gift_product_keyword/gift_product_keyword_list.jsp?product_no=" + product_no, "查看关键字", 800, 630);
	}
	
	function addAndEdit(index){
		if(undefined==index){//如果是增加
			toolbarAdd({
				width:800,
				height:630,
				title:"增加",
				params : {
	    			channel_uuid : "<%=channelUUID %>"
	    		}
			});
		}else{//如果是修改
	    	var row = grid.getRow(index);
	    	toolbarAdd({width:800,height:630,title:'编辑'},row.uuid);
		}
    }
	/*列表页条件查询*/
	function doSearch(){
		params = [];
		conditionStr = "";
		var no = jQuery("#no").val();
		if(''!=jQuery.trim(no)){
			conditionStr += " and no like '%"+no+"%' ";
		}
		var name = jQuery("#name").val();
		if(''!=jQuery.trim(name)){
			conditionStr += " and name like '%"+name+"%' ";
		}
		var category = jQuery("#category").val();
		if(''!=jQuery.trim(category)){
			conditionStr += " and category like '"+category+"%' ";
		}
		var brand = jQuery("#brand").val();
		if(''!=jQuery.trim(brand)){
			conditionStr += " and brand = '"+brand+"' ";
		}
		var is_rec = jQuery("#is_rec").val();
		if(''!=jQuery.trim(is_rec)){
			conditionStr += " and is_rec = '"+is_rec+"' ";
		}
		var is_show = $.trim($("#is_show").val());
		if(is_show!=""){
			conditionStr += " and is_show="+is_show;
		}
		var weight = $.trim($("#weight").val());
		if(weight!=""){
			conditionStr += " and weight="+weight;
		}
		params.push({'name':'tableName','value':tableName});
		params.push({'name':'enumNo','value':'106013'});
		params.push({'name':'fields','value':fields});
		params.push({'name':'whereStr','value':conditionStr});
		params.push({'name':'orderBy','value':orderBy});
		grid.options.parms=params;
		grid.loadData();
	}
	
/**
 * 新品推荐方法
 * 
 * @param {}
 *            arg 推荐时要传的参数
 */
function setRec(state) {
	var art;
	if (typeof(arg) == 'undefined' || arg == null) {
		arg = {};
	}
	var checkedRows = grid.getCheckedRows();
	if (checkedRows.length <= 0) {
		return;
	}
	// 遍历所有的选中行，如果是没有UUID，则移除
	jQuery.each(checkedRows, function(index, data) {
				if (typeof(this.uuid) == 'undefined') {
					jrDeleteRow(this.__index);
				}
			});
	jrConfirm("确定推荐选中项吗？", function(v) {
				if (v) {
					var checkedRows = grid.getCheckedRows();
					var idStr = "";
					for (var i = 0; i < checkedRows.length; i++) {
						idStr += "'" + checkedRows[i].uuid + "',";
					}
					if (idStr.indexOf(",") != -1) {
						idStr = idStr.substring(0, idStr.length - 1);
					}
					arg.del_id = idStr;
					if ('undefined' == typeof(arg.tableName)) {
						arg.tableName = tableName;
					}
					arg.state = state;
					executeRec(arg);
				}
			});
}


/**
 * 执行删除的方法
 * 
 * @param {}
 *            data 要传向后台的数据
 */
function executeRec(data) {
	var enumNo = '';
	for (var i = 0; i < params.length; i++) {
		if (params[i].name == 'enumNo') {
			enumNo = params[i].value;
		}
	}
	jQuery.ajax({
				url : '/backend/action/ajax_cud.jsp?enumNo=' + enumNo + '&flag=setRec',
				data : data,
				type : 'post',
				async : false,
				cache : false,
				success : function(data) {
					grid.loadData();
				}
			});
}

/**
 * 批量显示方法
 * 
 * @param {}
 *            arg 推荐时要传的参数
 */
function setShow(state) {
	var art;
	if (typeof(arg) == 'undefined' || arg == null) {
		arg = {};
	}
	var checkedRows = grid.getCheckedRows();
	if (checkedRows.length <= 0) {
		return;
	}
	// 遍历所有的选中行，如果是没有UUID，则移除
	jQuery.each(checkedRows, function(index, data) {
				if (typeof(this.uuid) == 'undefined') {
					jrDeleteRow(this.__index);
				}
			});
	var alerStr = "确定将所有选择产品设置为显示吗？";
	if(state=="0"){
		alerStr = "确定将所有选择产品设置为隐藏吗？";
	}
	jrConfirm(alerStr, function(v) {
				if (v) {
					var checkedRows = grid.getCheckedRows();
					var idStr = "";
					for (var i = 0; i < checkedRows.length; i++) {
						idStr += "'" + checkedRows[i].uuid + "',";
					}
					if (idStr.indexOf(",") != -1) {
						idStr = idStr.substring(0, idStr.length - 1);
					}
					arg.del_id = idStr;
					if ('undefined' == typeof(arg.tableName)) {
						arg.tableName = tableName;
					}
					arg.state = state;
					executeShow(arg);
				}
			});
}

/**
 * 执行显示的方法
 * 
 * @param {}
 *            data 要传向后台的数据
 */
function executeShow(data) {
	var enumNo = '';
	for (var i = 0; i < params.length; i++) {
		if (params[i].name == 'enumNo') {
			enumNo = params[i].value;
		}
	}
	jQuery.ajax({
				url : '/backend/action/ajax_cud.jsp?enumNo=' + enumNo + '&flag=setShow',
				data : data,
				type : 'post',
				async : false,
				cache : false,
				success : function(data) {
					grid.loadData();
				}
			});
}


/**
 * 批量上架方法
 * 
 * @param {}
 *            arg 推荐时要传的参数
 */
function setSale(state) {
	var art;
	if (typeof(arg) == 'undefined' || arg == null) {
		arg = {};
	}
	var checkedRows = grid.getCheckedRows();
	if (checkedRows.length <= 0) {
		return;
	}
	// 遍历所有的选中行，如果是没有UUID，则移除
	jQuery.each(checkedRows, function(index, data) {
				if (typeof(this.uuid) == 'undefined') {
					jrDeleteRow(this.__index);
				}
			});
	var alerStr = "确定将所有选择产品设置为上架吗？";
	if(state=="0"){
		alerStr = "确定将所有选择产品设置为下架吗？";
	}
	jrConfirm(alerStr, function(v) {
				if (v) {
					var checkedRows = grid.getCheckedRows();
					var idStr = "";
					for (var i = 0; i < checkedRows.length; i++) {
						idStr += "'" + checkedRows[i].uuid + "',";
					}
					if (idStr.indexOf(",") != -1) {
						idStr = idStr.substring(0, idStr.length - 1);
					}
					arg.del_id = idStr;
					if ('undefined' == typeof(arg.tableName)) {
						arg.tableName = tableName;
					}
					arg.state = state;
					executeSale(arg);
				}
			});
}

/**
 * 执行上架的方法
 * 
 * @param {}
 *            data 要传向后台的数据
 */
function executeSale(data) {
	var enumNo = '';
	for (var i = 0; i < params.length; i++) {
		if (params[i].name == 'enumNo') {
			enumNo = params[i].value;
		}
	}
	jQuery.ajax({
				url : '/backend/action/ajax_cud.jsp?enumNo=' + enumNo + '&flag=setSale',
				data : data,
				type : 'post',
				async : false,
				cache : false,
				success : function(data) {
					grid.loadData();
				}
			});
}

function copyProduct(id){
	if(confirm("确实要复制此产品吗？")){
		jQuery.ajax({
				url : '/backend/action/ajax_r.jsp?enumNo=106013&flag=copyProduct&id='+id,
				type : 'post',
				async : false,
				cache : false,
				success : function(data) {
					if($.trim(data)!=""&&$.trim(data)!="fail"){
						var proId = $.trim(data);
						$.ajax({
							type:"post",
							url:"/backend/action/ajax_r.jsp",
							data:{flag:"updateProductShortId", id:proId, enumNo:"106013"}
						});
						alert("复制成功");
						grid.loadData();
					}
				}
			});
	}
}
</script>